project(dmis-generator)
cmake_minimum_required( VERSION 2.8 )

set ( CMAKE_BUILD_TYPE Debug )
set ( EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/../../bin )
if( UNIX )
  add_definitions( -Wall -Wno-unused-function -Wno-write-strings )
endif()
include_directories ( ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )

# change the following line to true if you have made changes to debnf2pars.y or
# debnf2pars.lex and need to regenerate debnf2parsYACC.cc and/or debnf2parsLex.cc
set( REBUILD_PARSER false )

FIND_PACKAGE(BISON)
FIND_PACKAGE(FLEX)

if( BISON_FOUND AND FLEX_FOUND AND REBUILD_PARSER )
  message( "Will rebuild generated parser files with Flex and Bison." )
  BISON_TARGET( debnfParser debnf2pars.y ${PROJECT_BINARY_DIR}/debnf2parsYACC.cc )
  FLEX_TARGET( debnfScanner debnf2pars.lex ${PROJECT_BINARY_DIR}/debnf2parsLex.cc )
  ADD_FLEX_BISON_DEPENDENCY( debnfScanner debnfParser )

  # recent versions of bison need a header to be included; older versions fail with it included
  # we define BISON_TWO_FOUR so it can be checked in the code generated from debnf2pars.y
  # NOTE - this assumes that the behaviour change starts with 2.4.0
  string( COMPARE GREATER ${BISON_VERSION} 2.4 IS_BISON_TWENTYFOUR )
  if ( IS_BISON_TWENTYFOUR )
    message( "Bison 2.4 or later detected - will include header from debnf2parsYACC.cc" )
    add_definitions( -DBISON_TWO_FOUR )
  endif ()
  message( "Files generated by BISON: ${BISON_debnfParser_OUTPUTS}" )
  message( "Files generated by FLEX: ${FLEX_debnfScanner_OUTPUTS}" )
else()
  if( REBUILD_PARSER )
    message( FATAL_ERROR "Error, tried to regenerate parser but cannot find Bison and/or Flex!")
  endif()
  message( "Will not run Flex or Bison (this is normal)." )
  #not regenerating, so set these vars to the names of the already-generated files
  set( BISON_debnfParser_OUTPUTS ${CMAKE_SOURCE_DIR}/debnf2parsYACC.cc
                                 ${CMAKE_SOURCE_DIR}/debnf2parsYACC.hh )
  set( FLEX_debnfScanner_OUTPUTS ${CMAKE_SOURCE_DIR}/debnf2parsLex.cc )
endif()

include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_executable( debnf2pars
    ebnfClasses.hh ebnfClasses.cc
    ${BISON_debnfParser_OUTPUTS}
    ${FLEX_debnfScanner_OUTPUTS}
  )

add_executable( generateMore generateMore.cc )
